package com.wanghua.mall.secKill.controller;

import com.wanghua.common.utils.R;
import com.wanghua.mall.secKill.Dto.SkuRedisDto;
import com.wanghua.mall.secKill.service.SecKillService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wanghua
 * @create 2021-09-11 19:04
 */
@Controller
public class SecKillController {

    @Resource
    private SecKillService secKillService;

    /**
     * 获取当前正在秒杀的商品
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getCurrentSecKillSkus")
    public R getCurrentSecKillSkus() {
        List<SkuRedisDto> list = secKillService.getCurrentSecKillSkus();
        return R.ok().put("data", list);
    }


    /**
     * 获得秒杀的商品详细信息
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/getSecKillSkuInfo/{skuId}")
    public R getSecKillSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuRedisDto skuRedisDto = secKillService.getSecKillSkuInfo(skuId);
        return R.ok().put("data", skuRedisDto);
    }

    /**
     * 秒杀的请求
     *
     * @param killId
     * @param key
     * @param num
     * @return
     */
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num) {
        String orderNo = secKillService.secKill(killId, key, num);
        return "success";
    }
}
